// 示例使用
import React, { useState, useEffect } from "react";
import MyQuestionItem from "../User/UserQuestionItem";
import { getUserSendQuestionApi } from "../../http/module/User";
import { deleteQuestionApi } from "../../http/module/Question";
export default function MyQuestionList() {
  const [questions, setQuestions] = useState([]);
  const fetchQuestions = async () => {
    try {
      const response = await getUserSendQuestionApi();
      if (response.code == 200) {
        console.log(response.data.records);
        setQuestions(response.data.records);
      }
    } catch (error) {
      console.error("Error fetching questions:", error);
    }
  };
  useEffect(() => {
    fetchQuestions();
  }, []);
  const handleDelete = (id) => {
    console.log(id);

    // Call the delete API here
    deleteQuestionApi(id).then((response) => {
      if (response.code == 200) {
        setQuestions((prev) => prev.filter((q) => q.id !== id));
        alert("删除成功");
      }
    });
  };
  const handleUpdate = (updated) => {
    setQuestions((prev) =>
      prev.map((q) => (q.id === updated.id ? updated : q))
    );
  };

  return (
    <MyQuestionItem
      questions={questions}
      onDelete={handleDelete}
      onUpdate={handleUpdate}
    />
  );
}
